package com.laolei.redis.config;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.data.redis.support.collections.DefaultRedisMap;
import org.springframework.data.redis.support.collections.RedisProperties;

import com.laolei.redis.model.User;
import com.laolei.redis.repo.UserRepo;

@SpringBootApplication
@EnableRedisRepositories(basePackageClasses=UserRepo.class)
public class SpringRedisConfigApplication  implements CommandLineRunner {
	@Autowired UserRepo userRepo;
	@Autowired StringRedisTemplate redisTemplate;
	RedisProperties redisProperties;
	DefaultRedisMap<String, Object> redisMap;
	RedisAtomicLong atomicLong;
	
	public static void main(String[] args) {
		SpringApplication.run(SpringRedisConfigApplication.class, args);
	}
	@Override
	public void run(String... args) throws Exception {
		this.redisMap=new DefaultRedisMap<>("22222222222", redisTemplate);
		this.redisProperties=new RedisProperties(new Properties(), "44444444444444-key", redisTemplate);
		
//		userRepo.save(new User(2222, "张","三"));
//		userRepo.save(new User(2223, "张","三"));
//		userRepo.save(new User(2224, "张","三"));
		List<User> list=new ArrayList<>();
		userRepo.deleteAll();
		long s = System.currentTimeMillis();
		for (int i = 0; i < 1000000; i++) {
			list.add(new User(i, UUID.randomUUID().toString(),UUID.randomUUID().toString()));
			if(i%10==0){
				userRepo.save(list);
				list.clear();
				System.err.println((System.currentTimeMillis()-s)+"--"+i);
			}
		}
		userRepo.save(list);
		list.clear();
		System.err.println(System.currentTimeMillis()-s);
		
		System.err.println(userRepo.count());
		System.err.println(userRepo.findOne(2222));
		BoundSetOperations<String, String> keys = redisTemplate.boundSetOps(User.class.getName());
		for (String key : keys.members()) {
			System.err.println(redisTemplate.boundHashOps(User.class.getName()+":"+key).entries());
		}
		atomicLong=new RedisAtomicLong("number.1223", redisTemplate.getConnectionFactory());
		System.err.println(atomicLong.addAndGet(111));
		System.err.println(atomicLong.addAndGet(111));
		System.err.println(atomicLong.addAndGet(111));
		System.err.println(atomicLong.addAndGet(111));
	}
}
